﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;


using UnityEngine.Rendering;

namespace YYPostProcessing
{
    using UnityObject = UnityEngine.Object;
    public static class YYRuntimeUtilities
    {
        static Mesh s_FullscreenTriangle;
        static YYPostProcessResources s_Resources;

        public static Mesh fullscreenTriangle
        {
            get
            {
                if (s_FullscreenTriangle != null)
                    return s_FullscreenTriangle;

                s_FullscreenTriangle = new Mesh { name = "Fullscreen Triangle" };
                // Because we have to support older platforms (GLES2/3, DX9 etc) we can't do all of
                // this directly in the vertex shader using vertex ids :(
                s_FullscreenTriangle.SetVertices(new List<Vector3>
                {
                    new Vector3(-1f, -1f, 0f),
                    new Vector3(-1f,  3f, 0f),
                    new Vector3( 3f, -1f, 0f)
                });
                s_FullscreenTriangle.SetIndices(new[] { 0, 1, 2 }, MeshTopology.Triangles, 0, false);
                s_FullscreenTriangle.UploadMeshData(false);

                return s_FullscreenTriangle;
            }
        }

        static Material s_CopyStdMaterial;
        public static Material copyStdMaterial
        {
            get
            {
                if (s_CopyStdMaterial != null)
                    return s_CopyStdMaterial;

                //Assert.IsNotNull(s_Resources);
                //var shader = s_Resources.shaders.copyStd; context.resources.shaders.bloom
                //s_CopyStdMaterial = new Material(Shader.Find("YY/PostProcess/YYCopyStd"))
                s_CopyStdMaterial = new Material(s_Resources.shaders.copyStd)
                {
                    name = "PostProcess - CopyStd",
                    hideFlags = HideFlags.HideAndDontSave
                };

                return s_CopyStdMaterial;
            }
        }

        internal static void UpdateResources(YYPostProcessResources resources)
        {
            Destroy(s_CopyStdMaterial);
            s_CopyStdMaterial = null;
            s_Resources = resources;
        }




        public static void BlitFullscreenTriangle(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination
            ,YYPropertySheet yYPropertySheet,int pass)
        {
            cmd.SetGlobalTexture(YYShaderIDs.MainTex, source);
            cmd.SetRenderTarget(destination);

            cmd.DrawMesh(fullscreenTriangle, Matrix4x4.identity, yYPropertySheet.material, 0, pass, yYPropertySheet.properties);

        }

        public static void BuiltinBlit(this CommandBuffer cmd, RenderTargetIdentifier source, RenderTargetIdentifier destination, Material mat, int pass = 0)
        {
#if UNITY_2018_2_OR_NEWER
            cmd.SetRenderTarget(destination, RenderBufferLoadAction.DontCare, RenderBufferStoreAction.Store);
            destination = BuiltinRenderTextureType.CurrentActive;
#endif
            cmd.Blit(source, destination, mat, pass);
        }

        public static void Destroy(UnityObject obj)
        {
            if (obj != null)
            {
#if UNITY_EDITOR
                if (Application.isPlaying)
                    UnityObject.Destroy(obj);
                else
                    UnityObject.DestroyImmediate(obj);
#else
                UnityObject.Destroy(obj);
#endif
            }
        }
    }
}
